package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/captain/entity"
)

func UnlockTrainSeat(dB *gorm.DB, roleID, seat int32) entity.TrainSeat {

	var data = entity.TrainSeat{RoleID: roleID, Seat: seat}
	if err := dB.Create(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func GetTrainSeats(dB *gorm.DB, roleID int32) []entity.TrainSeat {

	var data []entity.TrainSeat
	if err := dB.Where("role_id = ?", roleID).Find(&data).Error; err != nil {
		panic(err)
	}

	if len(data) == 0 {

		data = append(data, UnlockTrainSeat(dB, roleID, 1))
	}

	return data
}

func GetTrainSeat(dB *gorm.DB, roleID, seat int32) entity.TrainSeat {

	var trainSeat = entity.TrainSeat{
		RoleID: roleID,
		Seat:   seat,
	}

	if err := dB.First(&trainSeat).Error; err != nil {
		panic(err)
	}

	return trainSeat
}

func SaveTrainSeat(db *gorm.DB, trainSeat *entity.TrainSeat) {

	if err := db.Save(&trainSeat).Error; err != nil {
		panic(err)
	}
}
